package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Special;
import com.admission.service.SpecialManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class SpecialAdminAction extends CrudActionSupport<Special> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Special entity;
	private SpecialManager specialManager;
	private String id;
	private Page<Special> page = new Page<Special>(PageUtils.DEFAULT_PAGE_SIZE);
	private File picture;
	private String pictureFileName;
	private String ids;
	private String newhome;
	private String newhightop;
	private String status;
	private String keyword;

	public Special getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filtersSpecial = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p = new PropertyFilter("LIKES_name", keyword);
			filtersSpecial.add(p);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		specialManager.getPage(page, filtersSpecial);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// ----------------上传图片方法------------------
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		File dir = new File(serverrealPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		String subPath = null;

		if (picture != null) {
			subPath = new String();
			subPath = NewFileName.genNewFileName(pictureFileName, 0);
			FileUtils.copyFile(picture, new File(serverrealPath, subPath));
		}
		try {
			specialManager.save(entity, subPath);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
			return INPUT;
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		specialManager.delete(ids);
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Special();
		} else {
			entity = specialManager.getSpecialById(id);
		}

	}

	/**
	 * 改变开放状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		specialManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}

	public Special getEntity() {
		return entity;
	}

	public void setEntity(Special entity) {
		this.entity = entity;
	}

	public SpecialManager getSpecialManager() {
		return specialManager;
	}

	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<Special> getpage() {
		return page;
	}

	public void setpage(Page<Special> page) {
		this.page = page;
	}

	public Page<Special> getPage() {
		return page;
	}

	public void setPage(Page<Special> page) {
		this.page = page;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getNewhome() {
		return newhome;
	}

	public void setNewhome(String newhome) {
		this.newhome = newhome;
	}

	public String getNewhightop() {
		return newhightop;
	}

	public void setNewhightop(String newhightop) {
		this.newhightop = newhightop;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}
